package com.logic.handler;

import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.logic.utils.LogicSrvUtils;
import com.pjc.config.RpcReturnCode;
import com.pjc.thrift.TInnerProto;

/**
 * 反射机制
 * 
 * @author 王畅
 * @since 2015年6月21日
 */
public class ProtoDispatcher {
	protected static final Logger LOG = LoggerFactory.getLogger(ProtoDispatcher.class);

	/**
	 * 分发协议
	 * 
	 * @author 王畅
	 * @param proto
	 * @return
	 * @throws Exception
	 */
	public static int dispatchProto(TInnerProto proto) throws Exception {
		try {
			Method method = LogicSrvUtils.getInstance().getMethod(proto.id);
			if (method == null) {
				return RpcReturnCode.NO_TARGET_METHOD;
			}
			Object data = method.invoke(null, new Object[] { proto.data });
			if (data == null) {
				return RpcReturnCode.OK;
			}
			return Integer.parseInt(data.toString());
		} catch (Exception ex) {
			LOG.error(ex.getMessage(), ex);
		}
		return RpcReturnCode.SYS_ERROR;
	}
}
